package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.query.TeacherQuery;
import com.atguigu.guli.service.edu.feign.OssFileService;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author liming
 * @since 2020-06-16
 */
@Slf4j
@RestController
//@CrossOrigin //支持跨域请求
@Api(tags = "讲师管理")
@RequestMapping("/admin/edu/teacher")
public class TeacherController {
    @Autowired
    TeacherService teacherService;

    @Autowired
    OssFileService ossFileService;

    @GetMapping("list")
    @ApiOperation("所有讲师列表")
    public R listAll() {
        log.info("所有讲师列表");
        log.warn("所有讲师列表");
        List<Teacher> list = teacherService.list();
        return R.ok().data("items", list).message("查询讲师列表成功");
    }

    @DeleteMapping("remove/{id}")
    @ApiOperation(value = "根据ID删除讲师",notes = "根据ID删除讲师")
    public R removeById(
            @ApiParam(value = "讲师Id",required = false)
            @PathVariable String id) {
        //删除头像  必须先删除头像否则查询不到对应id的讲师
        boolean b1 = teacherService.removerAvatarById(id);
        //删除讲师
        boolean b = teacherService.removeById(id);
        if (b) {
            return R.ok().message("删除成功");
        }else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("分页条件查询讲师列表")
    @GetMapping("list/{page}/{limit}")
    public R listPage(
            @ApiParam(value = "当前页码",required = true) @PathVariable long page,
            @ApiParam(value = "每页记录数",required = true) @PathVariable long limit,
            @ApiParam(value = "查询条件")TeacherQuery teacherQuery) {
        IPage<Teacher> pageModel = teacherService.selectPage(page, limit, teacherQuery);
        return R.ok().data("pageModel", pageModel);
    }

    @ApiOperation("新增讲师")
    @PostMapping("save")
    public R save(@ApiParam(value = "新增对象",required = true)@RequestBody Teacher teacher) {
        boolean b = teacherService.save(teacher);
        if (b) {
            return R.ok().message("添加成功");
        }else {
            return R.ok().message("添加失败");
        }
    }

    @ApiOperation("修改讲师")
    @PutMapping("update")
    public R update(@ApiParam(value = "修改对象",required = true)@RequestBody Teacher teacher) {
        boolean b = teacherService.updateById(teacher);
        if (b) {
            return R.ok().message("修改成功");
        }else {
            return R.ok().message("修改失败");
        }
    }

    @ApiOperation("根据id查询讲师")
    @GetMapping("get/{id}")
    public R queryById(@ApiParam(value = "讲师id",required = true)@PathVariable long id) {
        Teacher teacher = teacherService.getById(id);
        if (teacher != null) {
            return R.ok().data("item",teacher).message("查询成功");
        }else {
            return R.error().message("用户不存在");
        }
    }

    @ApiOperation("根据id列表批量删除")
    @DeleteMapping("batchDelete")
    public R batchDeleteById(
            @ApiParam(value = "讲师id列表",required = true)
            @RequestBody List<String> idList) {
        boolean b = teacherService.removeByIds(idList);
        if (b) {
            return R.ok().message("批量删除成功");
        }else{
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("根据左关键字查询讲师名列表")
    @GetMapping("list/name/{key}")
    public R selectNameListByKey(
            @ApiParam(value = "查询关键字", required = true)
            @PathVariable String key){

        List<Map<String, Object>> nameList = teacherService.selectNameListByKey(key);

        return R.ok().data("nameList", nameList);
    }

    @ApiOperation("测试服务调用")
    @GetMapping("test")
    public R testOOS() {
        ossFileService.test();
        return R.ok();
    }
}

